/**
 * Created by Ysssssss on 2021/4/12.
 */
import apiType, {
    GET_PRODUCT_LIST,
    GET_PRODUCT_PROPERTY_LIST,
    GET_PRODUCT_TYPE_LIST,
    GET_PRODUCT_TYPE_SELECTION,
    GET_PRODUCT_SELECTION,
    GET_PRODUCT_DETAIL_INFO,
    GET_PRODUCT_IMPORT_TASK_LIST,
    GET_SUPPLY_GROUP_LIST,
    GET_SUPPLY_GROUP_DETAIL_INFO,
    GET_SUPPLY_GROUP_PRODUCT_LIST,
    GET_PRODUCT_NATURE_STATS
} from "@constants/ActionTypes";
import {arrayToObject} from "@utils/arrayHelper";

const initialState = {
    productNatureStats: {},
    productListPagination: {},
    productSelectionPagination: {},
    productDetailMap: {},
    productTypeList: [],
    productTypeSelection: [],
    productPropertyList: [],
    productImportTaskListMap: {},
    supplyGroupList: [],
    supplyGroupMap: {},
    supplyGroupProductPagination: {}
}


export default function update(state = initialState, action) {
    const {response, params, payload} = action

    switch (action.type) {
        case apiType(GET_PRODUCT_NATURE_STATS).success:
            return {
                ...state,
                productNatureStats: arrayToObject(response.nature_list, 'nature', 'count')
            }
        case apiType(GET_PRODUCT_LIST).success:
            return {
                ...state,
                productListPagination: response
            }
        case apiType(GET_PRODUCT_SELECTION).success:
            return {
                ...state,
                productSelectionPagination: params.pageNo === 1 ? {
                    ...response,
                    pageNo: params.pageNo,
                } : {
                    ...state.productSelectionPagination,
                    prods: [
                        ...state.productSelectionPagination.prods,
                        ...response.prods
                    ],
                    pageNo: params.pageNo,
                }
            }
        case apiType(GET_PRODUCT_DETAIL_INFO).success:
            return {
                ...state,
                productDetailMap: {
                    ...state,
                    [response.id]: response
                }
            }
        case apiType(GET_PRODUCT_TYPE_LIST).success:
            return {
                ...state,
                productTypeList: response.prod_types
            }
        case apiType(GET_PRODUCT_TYPE_SELECTION).success:
            return {
                ...state,
                productTypeSelection: response.prod_types
            }
        case apiType(GET_PRODUCT_PROPERTY_LIST).success:
            return {
                ...state,
                productPropertyList: response.groups
            }
        case apiType(GET_PRODUCT_IMPORT_TASK_LIST).success:
            return {
                ...state,
                productImportTaskListMap: {
                    ...state.productImportTaskListMap,
                    [params.nature]: response.tasks
                }
            }
        case apiType(GET_SUPPLY_GROUP_LIST).success:
            return {
                ...state,
                supplyGroupList: response.menus
            }
        case apiType(GET_SUPPLY_GROUP_DETAIL_INFO).success:
            return {
                ...state,
                supplyGroupMap: {
                    ...state.supplyGroupMap,
                    [params.id]: response
                }
            }
        case apiType(GET_SUPPLY_GROUP_PRODUCT_LIST).success:
            return {
                ...state,
                supplyGroupProductPagination: response
            }
        default:
            return state
    }
}